// Generating Normalized Price Data For Structural Model
use ../dta/stock_monthly_vintage.dta, clear
keep code vintage date2 price

// Normalizing Price at date 590 (March 2009)
gen aux590=price if date2==590
bys code: egen aux2=max(aux590)
gen price590=price/aux2


// Creating Estimation Sample For All 5 Vintages (2 Pilot + 3 Standard)
gen estimation_sample=0
replace estimation_sample=1 if date2<=602 & vintage==0
replace estimation_sample=1 if date2<=623 & vintage==1
replace estimation_sample=1 if date2<=636 & vintage==2
replace estimation_sample=1 if date2<=644 & vintage==3
replace estimation_sample=1 if date2<=656 & vintage==4

// Creating A Dummy for Active Vintages
gen d=0
replace d=1 if date2>=602 & vintage==0
replace d=1 if date2>=623 & vintage==1
replace d=1 if date2>=636 & vintage==2
replace d=1 if date2>=644 & vintage==3
replace d=1 if date2>=656 & vintage==4

// Creating Count Down to Implementation Vintage by Vintage
gen t=0
replace t=602-date2 if vintage==0
replace t=623-date2 if vintage==1
replace t=636-date2 if vintage==2
replace t=644-date2 if vintage==3
replace t=656-date2 if vintage==4
replace t=-t
replace t=0 if t>=0

// Creating Price Differences
tsset code date2
gen price_diff=d.price590

// Creating Excess Price Changes Relative to Control Vintages
gen control=vintage==.
bys date2: egen control_diff_aux=mean(price_diff) if control==1
bys date2: egen control_diff=max(control_diff_aux)
gen excess_price_diff=price_diff-control_diff

// Creating Two-Way Residualized Price Changes
reghdfe price_diff , a(code 	date2) resid
predict price_diff_r_tw, r 

save ../dta/model_estimation_data.dta, replace
